/**
 * 前端使用axios请求后端urls
 */
import axiosInstance from './index'
import baseConfig from './base-config'

// const {URLSearchParams} = require('url')

const axios = axiosInstance
// 构建url，指和后端的url相同。可以在HelloWorld.urls文件中查看
const warpUrl = (s) => {
    if (s === undefined) s = '';
    return `${baseConfig.baseUrl}/${s}`
}
// 访问后端的/hello/ url
const getHello = () =>{
    return axios(
        warpUrl('hello/')
    )
}
// 获取每种类别的结点个数，如中药材：3911
const getNodesCount = (label) =>{
    return axios(
        warpUrl('info/nodes_count/'),
        {
            params:{
                'label':label//节点类型，如中药材
            }
        }
        
    );
}
// 根据类别搜索结点
const searchByNodeName = (selectMode,nodeName,selectType) =>{
    return axios(
        warpUrl('search/'),
        {
            params:{
                'selectMode':selectMode,//0:精确搜索；1：模糊搜索
                'nodeName':nodeName,//‘羌活’
                'selectType':selectType //‘中药材’
            }
        }
    )
}
//根据结点名称和类型获取结点属性（结果可能不唯一，因为名称可能会重复）
const getNodePropertyByName = (nodeName,selectType) =>{
    return axios(
        warpUrl('info/node/'),
        {
            params:{
                'nodeName':nodeName,//所有类型的结点，如药材，化合物等等
                'selectType':selectType//结点的类型
            }
        }
    )
}

//根据结点id获取结点属性（结果唯一，因为id唯一）
const getNodePropertyByID = (nodeID,selectType) =>{
    return axios(
        warpUrl('info/nodeMessage/'),
        {
            params:{
                'nodeID': nodeID, //结点ID，如herb00210
                'selectType':selectType//结点类型，如中药材
            }
        }
    )
}


//根据结点类型下载数据库结点
const downloadNode = (label) =>{
    return axios(
        warpUrl('download/node/'),
        {
            params:{
                'label':label
            }
        }
    )
}

//根据结点id和类别搜索与特定关系相连接的结点
//这里测试建议用nodeID:'formul00211',selectType:'中药组方',比较快
const getRelationNodes = (nodeID,selectType) =>{
    return axios(
        warpUrl('info/getRelationNodes/'),
        {
            params:{
                'nodeID': nodeID, //结点ID，如formul00211,即羌活胜湿汤
                'selectType':selectType//结点类型，如中药组方
            }
        }
    )
}

//根据类别返回统计数据
//返回：result['nodes_count'] 和 result['relations_count'] = relations_count_list    
const statistic =(nodeLabels,relationLabels) =>{
    const params = new URLSearchParams();
    params.append('nodes_label_list',nodeLabels.join(','));
    params.append('relations_label_list',relationLabels.join(','));
    return axios(
        warpUrl('info/statistic/'),
        {
            params
        }
    )
}
//根据标签放回text
//返回properities['id']和properities['content']
const getText =(text_label) =>{
    const params = new URLSearchParams();
    params.append('text_label',text_label);
    return axios(
        warpUrl('paper/getPaperText/'),
        {
            params
        }
    )
}
// 根据text返回实体抽取结果
// 返回result['response']
const getEntities =(text) =>{
    const params = new URLSearchParams();
    params.append('text',text);
    return axios(
        warpUrl('paper/getEntityResult/'),
        {
            params
        }
    )
}
// 根据text返回实体抽取结果
// 返回result['response']
const getRelations =(text) =>{
    const params = new URLSearchParams();
    params.append('text',text);
    return axios(
        warpUrl('paper/getRelationResult/'),
        {
            params
        }
    )
}



export {
    getHello,
    getNodePropertyByName,
    searchByNodeName,
    getNodesCount,
    downloadNode,
    getNodePropertyByID,
    getRelationNodes,
    statistic,
    getText,
    getEntities,
    getRelations
};



